/**
 * 作者: wgx
 * 日期: 2024-10-24 16:24:32
 * 备注: 用户建筑表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserBuild;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserBuildModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.build_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // 用户Id
    protected int buildId; // 建筑id
    protected long num; // 数量
    protected int level; // 等级
    protected int isUnlock; // 是否已解锁 (1-已解锁)
    protected int isPlace; // 是否摆放 (0-未摆放 1-已摆放)
    protected int recruitNum; // 招募次数
    protected int seatNum; // 可委派的干员数量
    protected String partner = ""; // 已委派的干员id列表
    protected long totalEarn; // 建筑总的赚速 ((经营收益+战力收益) * 各种加成不包括特权卡和兽潮)
    protected int partnerAdd2; // 派遣的干员技能加成百分比1W被

    public void reset() {
    }

    protected String getKey() {
        return "UserBuild." + userId + "." + buildId;
    }

    protected String getListKey() {
        return "UserBuild." + userId;
    }

    protected String getListItemKey() {
        return buildId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_build"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // 用户Id
        obj.put("buildId", buildId); // 建筑id
        obj.put("num", num); // 数量
        obj.put("level", level); // 等级
        obj.put("isUnlock", isUnlock); // 是否已解锁 (1-已解锁)
        obj.put("isPlace", isPlace); // 是否摆放 (0-未摆放 1-已摆放)
        obj.put("recruitNum", recruitNum); // 招募次数
        obj.put("seatNum", seatNum); // 可委派的干员数量
        obj.put("partner", partner); // 已委派的干员id列表
        obj.put("totalEarn", totalEarn); // 建筑总的赚速 ((经营收益+战力收益) * 各种加成不包括特权卡和兽潮)
        obj.put("partnerAdd2", partnerAdd2); // 派遣的干员技能加成百分比1W被
        return obj;
    }

    public PBUserBuild.Builder takeInitBuilder() {
        PBUserBuild.Builder sb = PBUserBuild.newBuilder();
        if (buildId != 0) {
            sb.setBuildId(buildId);
        }
        if (num != 0) {
            sb.setNum(num);
        }
        if (level != 0) {
            sb.setLevel(level);
        }
        if (isUnlock != 0) {
            sb.setIsUnlock(isUnlock);
        }
        if (isPlace != 0) {
            sb.setIsPlace(isPlace);
        }
        if (recruitNum != 0) {
            sb.setRecruitNum(recruitNum);
        }
        if (seatNum != 0) {
            sb.setSeatNum(seatNum);
        }
        if (this.partner != null && this.partner.length() > 0) {
            JsonArray list = new JsonArray(this.partner);
            for(int i=0; i<list.size(); i++) {
                sb.addPartner(list.getInteger(i));
            }
        }
        if (totalEarn != 0) {
            sb.setTotalEarn(totalEarn);
        }
        return sb;
    }

    public PBUserBuild.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserBuild.Builder sb = PBUserBuild.newBuilder();
        if (isFieldUpdate("buildId")) {
            sb.setBuildId(buildId);
        }
        if (isFieldUpdate("num")) {
            sb.setNum(num);
        }
        if (isFieldUpdate("level")) {
            sb.setLevel(level);
        }
        if (isFieldUpdate("isUnlock")) {
            sb.setIsUnlock(isUnlock);
        }
        if (isFieldUpdate("isPlace")) {
            sb.setIsPlace(isPlace);
        }
        if (isFieldUpdate("recruitNum")) {
            sb.setRecruitNum(recruitNum);
        }
        if (isFieldUpdate("seatNum")) {
            sb.setSeatNum(seatNum);
        }
        if (isFieldUpdate("partner")) {
            if (this.partner != null && this.partner.length() > 0) {
                JsonArray list = new JsonArray(this.partner);
                for(int i=0; i<list.size(); i++) {
                    sb.addPartner(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearPartner();
                }
            } else {
                sb.clearPartner();
            }
        }
        if (isFieldUpdate("totalEarn")) {
            sb.setTotalEarn(totalEarn);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getBuildId() {
        return buildId;
    }

    public void putBuildId(int buildId) {
        if (this.buildId != buildId) {
            this.buildId = buildId;
            updateRedisHashField(getKey(), "buildId", this.buildId);
        }
    }

    public void addBuildId(int value) {
        this.buildId += value;
        updateRedisHashField(getKey(), "buildId", this.buildId);
    }

    public long getNum() {
        return num;
    }

    public void putNum(long num) {
        if (this.num != num) {
            this.num = num;
            updateRedisHashField(getKey(), "num", this.num);
        }
    }

    public void addNum(long value) {
        this.num += value;
        updateRedisHashField(getKey(), "num", this.num);
    }

    public int getLevel() {
        return level;
    }

    public void putLevel(int level) {
        if (this.level != level) {
            this.level = level;
            updateRedisHashField(getKey(), "level", this.level);
        }
    }

    public void addLevel(int value) {
        this.level += value;
        updateRedisHashField(getKey(), "level", this.level);
    }

    public int getIsUnlock() {
        return isUnlock;
    }

    public void putIsUnlock(int isUnlock) {
        if (this.isUnlock != isUnlock) {
            this.isUnlock = isUnlock;
            updateRedisHashField(getKey(), "isUnlock", this.isUnlock);
        }
    }

    public void addIsUnlock(int value) {
        this.isUnlock += value;
        updateRedisHashField(getKey(), "isUnlock", this.isUnlock);
    }

    public int getIsPlace() {
        return isPlace;
    }

    public void putIsPlace(int isPlace) {
        if (this.isPlace != isPlace) {
            this.isPlace = isPlace;
            updateRedisHashField(getKey(), "isPlace", this.isPlace);
        }
    }

    public void addIsPlace(int value) {
        this.isPlace += value;
        updateRedisHashField(getKey(), "isPlace", this.isPlace);
    }

    public int getRecruitNum() {
        return recruitNum;
    }

    public void putRecruitNum(int recruitNum) {
        if (this.recruitNum != recruitNum) {
            this.recruitNum = recruitNum;
            updateRedisHashField(getKey(), "recruitNum", this.recruitNum);
        }
    }

    public void addRecruitNum(int value) {
        this.recruitNum += value;
        updateRedisHashField(getKey(), "recruitNum", this.recruitNum);
    }

    public int getSeatNum() {
        return seatNum;
    }

    public void putSeatNum(int seatNum) {
        if (this.seatNum != seatNum) {
            this.seatNum = seatNum;
            updateRedisHashField(getKey(), "seatNum", this.seatNum);
        }
    }

    public void addSeatNum(int value) {
        this.seatNum += value;
        updateRedisHashField(getKey(), "seatNum", this.seatNum);
    }

    public JsonArray getPartnerArray() {
        if (this.partner != null && this.partner.length() > 0) {
            return new JsonArray(this.partner);
        } else {
            return new JsonArray();
        }
    }

    public String getPartner() {
        return partner;
    }

    public void putPartner(String partner) {
        if (!this.partner.equals(partner)) {
            this.partner = partner;
            updateRedisHashField(getKey(), "partner", this.partner);
        }
    }

    public long getTotalEarn() {
        return totalEarn;
    }

    public void putTotalEarn(long totalEarn) {
        if (this.totalEarn != totalEarn) {
            this.totalEarn = totalEarn;
            updateRedisHashField(getKey(), "totalEarn", this.totalEarn);
        }
    }

    public void addTotalEarn(long value) {
        this.totalEarn += value;
        updateRedisHashField(getKey(), "totalEarn", this.totalEarn);
    }

    public int getPartnerAdd2() {
        return partnerAdd2;
    }

    public void putPartnerAdd2(int partnerAdd2) {
        if (this.partnerAdd2 != partnerAdd2) {
            this.partnerAdd2 = partnerAdd2;
            updateRedisHashField(getKey(), "partnerAdd2", this.partnerAdd2);
        }
    }

    public void addPartnerAdd2(int value) {
        this.partnerAdd2 += value;
        updateRedisHashField(getKey(), "partnerAdd2", this.partnerAdd2);
    }

}